<template>
  <div>
    <div class="tools-div">
      <el-button type="success" size="small" @click="addShowAddDialog">
        添 加
      </el-button>
    </div>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="name" label="品牌名称" />
      <el-table-column prop="logo" label="品牌图标" #default="scope">
        <img :src="scope.row.logo" width="50" />
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="200" #default="scope">
        <el-button
          type="primary"
          size="small"
          @click="handleShowUpdateDialog(scope.row)"
        >
          修改
        </el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[1, 2, 3, 4, 5]"
      layout="total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleChange"
      @current-change="handleChange"
    />
    <!--添加对话框-->
    <div>
      <el-dialog v-model="dialogVisible" :title="title" width="30%">
        <el-form label-width="120px">
          <el-form-item label="品牌名称">
            <el-input v-model="brand.name" />
          </el-form-item>
          <el-form-item label="品牌图标">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:8501/admin/system/fileUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :headers="headers"
            >
              <img v-if="brand.logo" :src="brand.logo" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveOrUpdate">
              提交
            </el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listBrands, Save, DeleteById, GetById, UpdateById } from '@/api/brand'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useApp } from '@/pinia/modules/app'
// 定义表格数据模型
const list = ref([])
let pageNum = ref(1)
let pageSize = ref(3)
let title = ref('新增品牌')
// 分页条数据模型
const total = ref(0)
let dialogVisible = ref(false)
//定义表单对象
const brand = ref({})
onMounted(() => {
  fetchdata()
})
// 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
const headers = {
  token: useApp().authorization.token,
}
// 图像上传成功以后的事件处理函数
const handleAvatarSuccess = (response, uploadFile) => {
  brand.value.logo = response.data
}
//显示数据
const fetchdata = async () => {
  const { data } = await listBrands(pageNum.value, pageSize.value)
  list.value = data.list
  total.value = data.total
}
//页码改变时的事件
const handleChange = () => {
  fetchdata()
}
// 显示添加品牌表单
const addShowAddDialog = () => {
  brand.value = {}
  title.value = '添加品牌'
  dialogVisible.value = true
}
//删除按钮的事件函数
const handleDelete = row => {
  ElMessageBox.confirm(`您确认删除【${row.name}】吗?`, '删除品牌', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      //异步请求删除id对应的角色
      const { code, message } = await DeleteById(row.id)
      if (+code === 200) {
        ElMessage.success(message)
        fetchdata()
      } else {
        ElMessage.error(message)
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
//新增
const handleSave = async () => {
  await Save(brand.value)
  ElMessage.success('新增成功')
  dialogVisible.value = false
  fetchdata()
}
//新增或者提交的事件函数
const handleSaveOrUpdate = () => {
  if (brand.value.id) {
    //更新
    handleUpdate()
  } else {
    //新增
    handleSave()
  }
}
//更新
const handleUpdate = async () => {
  const { code, message } = await UpdateById(brand.value)
  ElMessage.success('更新成功')
  dialogVisible.value = false
  fetchdata()
}
//更新功能
const handleShowUpdateDialog = async row => {
  title.value = '更新品牌'
  dialogVisible.value = true
  const { code, message, data } = await GetById(row.id)
  brand.value = data
}
</script>

<style scoped>
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
